

jQuery(document).ready(function() {
	
	initTable();
	
	App.setPage("dict");  //Set current page
	App.init(); //Initialise plugins and elements
	

	//列表查询事件
	$('#search').click(function(){
		$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	
	
	//新增字典
	$('#addDict').click(function(){
    	setModalSize();
    	
    	$('#ifModal').attr("src", "add.html");
    	
		$('#modalTile').html("字典新增");
		$("#modal").modal('show');
	});
	
	//批量删除
	$('#batchDelete').click(function(){
		
		var data = $('#table').bootstrapTable('getSelections');
		if(data.length <= 0){
			window.wxc.xcConfirm('请勾选要<strong>批量删除</strong>的字典数据', window.wxc.xcConfirm.typeEnum.info);
			return;
		}
		var dictcodes = "";
		for(var i = 0; i < data.length; i++){
			dictcodes += data[i].dictcode + ",";
		}
		
		dictcodes = dictcodes.substring(0, dictcodes.length - 1);
		window.wxc.xcConfirm('是否<strong>批量删除</strong>字典数据?<br /><span style="color:red;">['+ dictcodes +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'dict/batchDelete.do',
					data: {"dictcodes": dictcodes},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){        							        							
	        	            window.wxc.xcConfirm('删除字典['+ data.datas[0].dictcode +']成功!', window.wxc.xcConfirm.typeEnum.info);
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});
	});
	
	//保存数据事件
	$('#save').click(function(){
		$("#ifModal")[0].contentWindow.save();
	});
	
	
	//清空表单数据事件
	$("#modalDict").on("hidden.bs.modal", function() {
		$('#tableDictDetail').find('tbody').find('tr').not('.no-records-found').remove();	
		if($('#' + tableID).find('tbody').children('tr').length == 0){
			var trStr = '<tr class="no-records-found">	'+
		    '   <td colspan="7" style="font-size:14px;">			'+
			'		单充底部“新增行”增加字典！		'+
			'	</td>						'+
			'</tr>';
			
			$('#' + tableID).find('tbody').append(trStr);
		}
		
		$(this).find('[type="text"]').val('');
		$(this).find('[type="checkbox"]').prop("checked",true);         	
	});
});		



function initTable(){		
	$('#table').bootstrapTable({
		method: 'post',
	    url: basePath() + 'dict/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	dictcode: $('#dictcode').val(),
                	dictname: $('#dictname').val(),
                	ifallow: $('#ifallow').is(':checked') ? 1 : 0
                };
        },
	    columns: [{
            field : 'checked',
            checkbox : true

        }, {  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) {  
// 		                return index + 1;  
                var page = $('#table').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'dictcode',
	        title: '字典代码'
	    }, {
	        field: 'dictname',
	        title: '字典名称'
	    }, {
	        field: 'ifallow',
	        title: '是否可维护',
	        formatter:function(value,row,index){
	        	var result = value;
	        	
	        	if(value == "1")
	        		result = "是";
	        	else 							
        			result = "否";
	        	return result;
	        }
	    },  {
            field: 'operate',
            title: '操作',
            align: 'center',
            width: '300px',
            events: operateEvents,
            formatter: operateFormatter
        } ]
	});
}

function operateFormatter(value, row, index) {
    return [
        '<button type="button" class="Edit btn btn-warning  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
        '<button type="button" class="Delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>'
    ].join('');
}

window.operateEvents = {
    'click .Edit': function (e, value, row, index) {
    	var dictcode = row.dictcode;
    	
    	setModalSize();
    	
    	$('#ifModal').attr("src", "edit.html?dictcode=" + dictcode);
    	
		$('#modalTile').html("字典修改");
		$("#modal").modal('show');
    },
    'click .Delete': function (e, value, row, index) {

		var dictcode = row.dictcode;
		window.wxc.xcConfirm('是否<strong>删除</strong>字典数据?<br /><span style="color:red;">['+ dictcode +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'dict/delete.do',
					data: {"dictcode": dictcode},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){
				        	$('#table').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});
    }
 };

